/*
* 滚动到页面顶部
* */
export function scrollToTop() {
  let timer = null
  cancelAnimationFrame(timer)
  timer = requestAnimationFrame(function fn () {
    let oTop = document.body.scrollTop || document.documentElement.scrollTop
    if (oTop > 0) {
      document.body.scrollTop = document.documentElement.scrollTop = oTop - 150
      timer = requestAnimationFrame(fn)
    } else {
      cancelAnimationFrame(timer)
    }
  })
}

/*
* 当滚动的高度高于浏览器视口高度的一半时，显示滚动到顶部按钮
* showScrollBtn：表示按钮的显示与否
* */

// 获取滚动条在Y轴上的滚动距离
function getScrollTop () {
  let scrollTop = 0
  let bodyScrollTop = 0
  let documentScrollTop = 0
  if (document.body) {
    bodyScrollTop = document.body.scrollTop
  }
  if (document.documentElement) {
    documentScrollTop = document.documentElement.scrollTop
  }
  scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop
  return scrollTop
}

// 监听浏览器的滚动行为
window.addEventListener('scroll', () => {
  // window.innerHeight为浏览器视口的高度
  if (getScrollTop() * 2 > window.innerHeight) {
    // 显示 showScrollBtn = true
  } else {
    // 隐藏 showScrollBtn = false
  }
})
